<template>
  <div>

    <el-card class="unit-detail-card">

      <i class="iconfont icon-danwei" />
      <div class="card-panel-description">
        <div class="card-panel-text">
          单位名称：{{ currentUnit.name }}
        </div>
        <div class="card-panel-text">
          单位地址：{{ currentUnit.address }}
        </div>
      </div>
    </el-card>

    <div>
      <el-row :gutter="20">
        <el-col :span="6">
          <el-card
            shadow="hover"
            class="unit-device-card"
            @click.native="toDeviceTable()"
          >
            <i class="iconfont icon-shebei" />
            <div class="card-panel-description">
              <div class="card-panel-text">
                设备总数量
              </div>
              <span class="card-panel-num">{{ totalDeviceCount }}</span>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card
            shadow="hover"
            class="unit-device-card"
            @click.native="toDeviceTable(2)"
          >
            <i class="iconfont icon-yichang" />
            <div class="card-panel-description">
              <div class="card-panel-text">
                异常设备
              </div>
              <span class="card-panel-num">{{ errorDeviceCount }}</span>
            </div>
          </el-card>
        </el-col>

      </el-row>

    </div>
  </div>

</template>

<script>
import { getDeviceCount } from '@/api/device'

export default {
  name: 'Index',
  props: ['unit', 'fromMap'],
  data() {
    return {
      queryVo: {
        query: {
          status: -1,
          unitId: null
        }
      },
      currentUnit: {},

      errorDeviceCount: 0,

      totalDeviceCount: 0
    }
  },
  mounted() {
    if (this.fromMap) {
      localStorage.removeItem('currentUnit')
    }
    const currentUnit = localStorage.getItem('currentUnit')

    if (currentUnit) {
      this.currentUnit = JSON.parse(currentUnit)

      this.queryVo.query.unitId = this.currentUnit.id
    } else {
      this.currentUnit = this.$route.params.unit
      this.queryVo.query.unitId = this.currentUnit.id

      localStorage.setItem('currentUnit', JSON.stringify(this.$route.params.unit))
    }

    getDeviceCount(this.queryVo).then(response => {
      this.totalDeviceCount = response.data.deviceCount
      this.queryVo.query.status = 2

      getDeviceCount(this.queryVo).then(response => {
        this.errorDeviceCount = response.data.deviceCount
        this.queryVo.query.status = -1
      })
    })
  },

  beforeDestroy() {

  },

  methods: {

    toDeviceTable(val) {
      if (val === 2) {
        this.$router.push({
          name: 'device-table',
          params: {
            unitId: this.currentUnit.id,
            status: 2
          }
        })
      } else {
        this.$router.push({
          name: 'device-table',
          params: {
            unitId: this.currentUnit.id

          }
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.unit-detail-card {
  .card-panel-description {
    float: left;
    font-weight: bold;
    margin: 26px;
    margin-left: 0px;
    .card-panel-text {
      line-height: 18px;
      color: rgba(0, 0, 0, 0.45);
      font-size: 16px;
      margin-bottom: 12px;
    }

  }

}
.unit-device-card {
  width: 250px;
  height: 150px;
  margin-top: 50px;
  margin-left: 40px;
}
.card-panel-description {
  float: right;
  font-weight: bold;
  margin: 26px;
  margin-left: 0px;
  .card-panel-text {
    line-height: 18px;
    color: rgba(0, 0, 0, 0.45);
    font-size: 16px;
    margin-bottom: 12px;
  }
  .card-panel-num {
    font-size: 20px;
  }

}
.unit-image {
    float: left;
  margin-right: 50px;
    width: 100px;
    height: 100px;
}
.icon-danwei {
  font-size: 90px;
  float: left;
  margin-right: 50px;
  margin-left: 50px;
  width: 100px;
  height: 100px;
}
.icon-shebei {
  font-size: 90px;
}
.icon-yichang {
  font-size: 90px;
}
.image {

  width: 100px;
  height: 100px;
}
</style>
